package cn.wolfcode.wms.domain;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class Product extends BaseDomain {
    // 商品名称
    private String name;
    // 商品编号
    private String sn;
    // 成本价
    private BigDecimal costPrice;
    // 市场价
    private BigDecimal salePrice;
    // 商品图片
    private String imagePath;
    // 商品介绍
    private String intro;
    // 关联的品牌id
    private Long brandId;
    // 品牌名称
    private String brandName;

    // {id: 1, name: iPhoneX, costPrice:10, brandName: 360}
    public String getJson() {
        Map<String, Object> json = new HashMap<>();
        json.put("id", getId());
        json.put("name", name);
        json.put("costPrice", costPrice);
        json.put("brandName", brandName);
        return JSON.toJSONString(json);
    }

    public String getSmallImagePath() {
        if (StringUtils.isEmpty(imagePath)) {
            return "";
        }

        // 得到图片前缀
        String prefix = this.imagePath.substring(0, this.imagePath.lastIndexOf("."));
        // 得带图片后缀
        String suffix = this.imagePath.substring(this.imagePath.lastIndexOf("."));
        return prefix + "_small" + suffix;
    }
}